angular.module('node', []).controller('nodeCtrl', [
    '$scope',
    'httpService',
    'alertmsg',
    function($scope, httpService, alertmsg) {
        /*
      获取nodes和departments start
      参数：company_id
    */
        $scope.getNodesAndDepartment = function() {
            httpService.query('/web/companies/' + user.company_id + '/fetch_nodes').then(function(res) {
                if (res.code == 200) {
                    $scope.nodes = res.nodes;
                } else {
                    alertmsg(res.msg);
                }
            });

            httpService.query('web/companies/' + user.company_id + '/fetch_departments', {limit: 999}).then(function(res) {
                if (res.code == 200) {
                    $scope.departments = res.departments;
                } else {
                    alertmsg(res.msg);
                }
            });

            $scope.selected_users = [];
            $scope.node = {
                department_id: null,
                department_role_id: null
            }
        }
        $scope.getNodesAndDepartment();
        /*  获取nodes和departments end */

        /*
      通过的部门的修改内容来获取角色的列表 start
      参数：
    */
        $scope.changeDepartment = function(modalType) {
            if ($scope.node.department_id || (modalType == 'edit' && $scope.edit_node.department_id)) {
                var data = !modalType
                    ? {
                        department_id: $scope.node.department_id
                    }
                    : {
                        department_id: $scope.edit_node.department_id
                    }
                httpService.query('/web/companies/' + user.company_id + '/fetch_roles', data).then(function(res) {
                    if (res.code == 200) {
                        $scope.department_roles = res.roles;
                        $('#department_role_id').attr('disabled', false)
                    } else {
                        alertmsg(res.msg);
                    }
                });
                $scope.selected_users = []
            } else {
                $('#department_role_id').attr('disabled', true)
            }
        }
        /*  通过的部门的修改内容来获取角色的列表 end */

        /*
      修改选中的部门角色 start
      参数：
    */
        $scope.changeRole = function(modalType) {
            if ($scope.node.department_role_id || (modalType == 'edit' && $scope.edit_node.depart_role_id)) {
                var data = !modalType
                    ? {
                        department_role_id: $scope.node.department_role_id
                    }
                    : {
                        department_role_id: $scope.edit_node.depart_role_id
                    }
                httpService.query('/web/companies/' + user.company_id + '/fetch_role_users', data).then(function(res) {
                    if (res.code == 200) {
                        $scope.users = res.users;
                        $('#user_ids').attr('disabled', false)
                    } else {
                        alertmsg(res.msg);
                    }
                });
                $scope.selected_users = []
            } else {
                $('#user_ids').attr('disabled', true)
            }
        }
        /*  修改选中的部门角色 end */

        /*
        选中审核人的方法 start
        参数：
    */
        $scope.selectUser = function(modalType) {
            if ($scope.node.user || (modalType == 'edit' && $scope.edit_node.user)) {
                var selected_ids = []
                angular.forEach($scope.selected_users, function(user, key) {
                    selected_ids.push(user.id)
                });
                if ((modalType == 'edit' && selected_ids.indexOf(parseInt($scope.edit_node.user.id)) != -1) || (!modalType && selected_ids.indexOf(parseInt($scope.node.user.id)) != -1)) {
                    alert('审核人已被添加过！')
                } else {
                    if (modalType) {
                        $scope.selected_users.push($scope.edit_node.user)
                    } else {
                        $scope.selected_users.push($scope.node.user)
                    }
                }
            } else {}
        }
        /*  选中审核人的方法 end  */

        /*
            删除选中的用户 start
            参数：index
        */
        $scope.deleteSelectedUsers = function(modalType, key) {
            if (modalType == 'edit'){
                httpService.query('web/companies/' + user.company_id + '/edit_node', {
                    node_id: $scope.edit_node.id,
                    users: [$scope.selected_users[key]],
                    type: 'delete'
                }, 'post').then(function(res) {
                    if (res.code == 200) {
                      alert('审核人已删除！');
                      if ($scope.selected_users.length <= 0) {
                        $('#editNodeModal').modal('hide');
                      }
                    } else {
                        alertmsg(res.msg);
                    }
                });
            }
            $scope.selected_users.splice(key, 1);
        }
        /*  删除选中的用户 end  */

        /*
            清空selected_users start
            参数：
        */
        $scope.cleanSelectedUsers = function() {
            $scope.selected_users = [];
        }
        /*  清空selected_users end  */

        /*
            新建审核节点 start
            参数：
        */
        $scope.newNode = function() {
            if ($scope.node.department_id && $scope.node.department_role_id && $scope.selected_users.length > 0) {
                var users = []
                angular.forEach($scope.selected_users, function(value, key) {
                    users.push(value.id)
                });
                var nodeData = {
                    department_id: $scope.node.department_id,
                    depart_role_id: $scope.node.department_role_id,
                    users: users,
                    level: $scope.nodes.length || 0
                }
                httpService.query('/web/companies/' + user.company_id + '/add_node', nodeData, 'post').then(function(res) {
                    if (res.code == 200) {
                        $('#newNodeModal').modal('hide');
                        $scope.getNodesAndDepartment();
                    } else {
                        alertmsg(res.msg);
                    }
                });
            } else {
                alert('请按部选择！')
            }
        }
        /*  新建审核节点 end  */

        /*
      删除审核节点 start
      参数：id
    */
        $scope.deleteNode = function(id) {
            httpService.query('web/companies/' + user.company_id + '/edit_node', {
                node_id: id,
                users: [],
                type: 'delete'
            }, 'post').then(function(res) {
                if (res.code == 200) {
                    httpService.query('web/companies/' + user.company_id + '/delete_node', {
                        node_id: id
                    }, 'post').then(function(res) {
                        if (res.code == 200) {
                            $scope.getNodesAndDepartment();
                        } else {
                            alertmsg(res.msg);
                        }
                    });
                } else {
                    alertmsg(res.msg);
                }
            });
        }
        /*  删除审核节点 end */

        /*
      打开修改的modal start
      参数：id
    */
        $scope.editNode = function(index) {
            $('#editNodeModal').modal('toggle')
            $scope.edit_node = $scope.nodes[index]
            $scope.changeDepartment('edit')
            $scope.changeRole('edit')
            $scope.selected_users = $scope.edit_node.users
        }
        /*  打开修改的modal end */

        /*
      提交修改操作并且关闭modal start
      参数：id
    */
        $scope.editedNode = function() {
            var users = []
            angular.forEach($scope.selected_users, function(value, key) {
              users.push(value.id)
            });
            var data = {
                node_id: $scope.edit_node.id,
                users: users,
                type: 'add'
            }
            httpService.query('/web/companies/' + user.company_id + '/edit_node', data, 'post').then(function(res) {
                if (res.code == 200) {
                    $('#editNodeModal').modal('hide');
                    $scope.getNodesAndDepartment();
                } else {
                    alertmsg(res.msg);
                }
            });
        }
        /*  提交修改操作并且关闭modal end */
    }
]);
